//
//  ARTWelcomeController.m
//  ChartsDemo
//
//  Created by wusiping on 2021/3/29.
//

#import "ARTWelcomeController.h"
#import "AppDelegate.h"

@interface ARTWelcomeController ()

@property (nonatomic,strong) UIView *cicleBg;
@property (nonatomic,strong) UILabel *skipLabel;
@property (nonatomic,strong) UILabel *timeLabel;

@property (nonatomic,strong) UIImageView *bgView;
@property (nonatomic,strong) UIImageView *titleImg;
@property (nonatomic,strong) UILabel *detailLabel;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) NSInteger totalTime;

@end

@implementation ARTWelcomeController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.timer invalidate];
    self.timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UIConfig];
    [self dataConfig];
}

#pragma mark - dataConfig
- (void)dataConfig
{
    self.totalTime = 3;
    [self configSkipLabel];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; //将NSTimer放在forMode:NSRunLoopCommonModes模式下，在任何情况下都能够正常工作
}

- (void)countTime
{
    if (self.totalTime == 0) {
        [kAppDelegate.launchWindow resignKeyWindow];
        kAppDelegate.launchWindow = nil;
    }else {
        self.totalTime = self.totalTime - 1;
        [self configSkipLabel];
    }
}

- (void)configSkipLabel
{
    NSString *string = [NSString stringWithFormat:@"%@%lds",FYLocalizedString(@"app__skip", @""),(long)self.totalTime];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    [text addAttribute:NSForegroundColorAttributeName value:J_ColorFromHEX(0x0095B6) range:NSMakeRange(FYLocalizedString(@"app__skip", @"").length, 2)];
    _skipLabel.attributedText = text;
}

#pragma mark - UIConfig
- (void)UIConfig
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.cicleBg];
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSkip)];
    [self.cicleBg addGestureRecognizer:tap];
    [self.cicleBg addSubview:self.skipLabel];
//    [self.cicleBg addSubview:self.timeLabel];

    [self.view addSubview:self.bgView];
    [self.view addSubview:self.detailLabel];
    [self.view addSubview:self.titleImg];

    [_cicleBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(66);
        make.top.mas_equalTo(STATUS_HEIGHT + 20);
        make.right.mas_equalTo(-30);
    }];
    
    [_skipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.cicleBg);
        make.width.mas_equalTo(66);
        make.height.mas_equalTo(20);
    }];
    
    CGFloat h = 375/XCScreenW*419;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(h);
        
    }];
    
    _bgView.image = [UIImage imageNamed:@"default_product"];
    
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-64);
        make.height.mas_equalTo(12);
    }];
    _detailLabel.text = FYLocalizedString(@"app__company_name", @"");
    
    [_titleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.detailLabel.mas_top).offset(-15);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    _titleImg.image = [UIImage imageNamed:@"default_logo1"];
    
    
}

- (UIView *)cicleBg
{
    if (!_cicleBg) {
        _cicleBg = [[UIView alloc] init];
        [_cicleBg setBackgroundColor:[UIColor whiteColor]];
        _cicleBg.layer.cornerRadius = 33;
        _cicleBg.layer.borderWidth = 1.0f;
        _cicleBg.layer.borderColor = J_ColorFromHEX(0xCCCCCC).CGColor;
    }
    return _cicleBg;
}

- (UILabel *)skipLabel
{
    if (!_skipLabel) {
        _skipLabel = [[UILabel alloc] init];
        _skipLabel.textColor = J_ColorFromHEX(0x999999);
        _skipLabel.font = [UIFont systemFontOfSize:14];
        _skipLabel.textAlignment = NSTextAlignmentCenter;
        _skipLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _skipLabel;
}

- (UIImageView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        
    }
    return _bgView;
}

- (UIImageView *)titleImg
{
    if (!_titleImg) {
        _titleImg = [[UIImageView alloc] init];
        _titleImg.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _titleImg;
}

- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:12];
        _detailLabel.textColor = J_ColorFromHEX(0x222222);
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _detailLabel;
}

#pragma mark - refresh
- (void)refresh
{
    
}

- (void)touchSkip
{
    [kAppDelegate.launchWindow resignKeyWindow];
    kAppDelegate.launchWindow = nil;
}

@end
